<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head><link rel="stylesheet" type="text/css" href="description/Combined.css,0:HeaderFooterSprite,0:Header.NonMtps,1:LinkList;/Areas/Centers/Themes/StandardDevCenter/Content:0,/Areas/Epx/Themes/Base/Content:1&amp;amp;hashKey=C9973DA951AE6202C9B348379A1BE49D" xmlns="http://www.w3.org/1999/xhtml" />
<link type="text/css" rel="stylesheet" xmlns="http://www.w3.org/1999/xhtml" />
<link rel="stylesheet" type="text/css" href="description/b79636be-b088-48b5-8a8d-c21645d26d2fCombined.css,0:HeaderFooterSprite,0:Footer.NonMtps,1:LinkList;/Areas/Centers/Themes/StandardDevCenter/Content:0,/Areas/Epx/Themes/Base/Content:1&amp;amp;hashKey=F576C687BC536B84D6E5B3246EE39B49" xmlns="http://www.w3.org/1999/xhtml" />
<link type="text/css" rel="stylesheet" xmlns="http://www.w3.org/1999/xhtml" />

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Windows Workflow Foundation (WF4) - Tracking State Machine</title>
        <link href="description/Galleries.css" type="text/css" rel="Stylesheet" /><link href="description/Layout.css" type="text/css" rel="Stylesheet" /><link href="description/Brand.css" type="text/css" rel="Stylesheet" />
        <link href="description/iframedescription.css" rel="Stylesheet" type="text/css" />
        <script src="description/offline.js" type="text/javascript"></script>
        <style type="text/css">
            #projectInfo {
                overflow: auto;
            }
            #longDesc {
                clear:both;
                margin: 25px 0 10px 0;
            }

            #SampleIndexList{
                margin-left: 15px;
            }
        </style>
    </head>
<body>
    <div id="offlineDescription">
        <h1>Windows Workflow Foundation (WF4) - Tracking State Machine</h1>
        <br/>
        <div id="projectInfo">
            <div class="section">
                    <div class="itemBarLong tagsContainer">
                        <label for="Technologies">Technologies</label>
                        <div id="Technologies">
                            Windows Workflow, WF4
                        </div>
                    </div>
                    <div class="itemBarLong tagsContainer">
                        <label for="Topics">Topics</label>
                        <div id="Topics">
                            State Machine
                        </div>
                    </div>
                <div class="itemBarLong">
                    <label for="Platforms">Platforms</label>
                    <div id="Platforms">
                        Desktop, Web, Cloud
                    </div>
                </div>
                <div class="itemBarLong">
                    <label for="Requirements">Requirements</label>
                    <div id="Requirements">
                        
                    </div>
                </div>
                <div class="itemBar">
                    <label for="LastUpdated">Primary language</label>
                    <div id="LastUpdated">en-US</div>
                </div>
                <div class="itemBar">
                    <label for="LastUpdated">Updated</label>
                    <div id="LastUpdated">6/27/2012</div>
                </div>
                <div class="itemBarLong">
                    <label for="License">License</label>
                    <div id="License">
                        <a href="license.rtf">Apache License, Version 2.0</a></div>
                </div>
                <div class="itemBar">
                    <div class="viewonlinecont">
                        <a data-link="online" href="http://code.msdn.microsoft.com/Windows-Workflow-fee72008">View this sample online</a>
                    </div>
                </div>
            </div>
        </div>
        
                   
<script type="text/javascript">
    function initializePage() {
        var otherTabClass = 'otherTab';
        var hiddenPreClass = 'hidden';

        var htmlDecode = function(encodedData) {
            var decodedData = "";
            if (encodedData) {
                var div = document.createElement('div');
                div.innerHTML = encodedData;
                decodedData = div.firstChild.nodeValue.replace( /\\r\\n/ig , '\r\n');
            }
            return decodedData;
        };
                
        Galleries.iterateElem(Galleries.findElem(null, 'div', 'scriptcode'), function (index, scriptBlock) {
            var titleElem = Galleries.findElem(scriptBlock, 'div', 'title')[0];
            var labelElems = Galleries.findElem(titleElem, 'span');
            if (labelElems.length == 0) {
                labelElems = titleElem;
            }
            var languageSpans = Galleries.findElem(scriptBlock, 'span', 'hidden');
            var pres = Galleries.findElem(scriptBlock, 'pre');
            if (languageSpans.length > 0 && pres.length > 1) {
                Galleries.iterateElem(labelElems, function(index, elem) {
                    var codePre = pres[index];
                    var labelSpan = elem;
                    var languageSpan = languageSpans[index];
                            
                    elem.code = codePre.innerHTML.replace( /(\r(\n)?)|((\r)?\n)/ig , '\\r\\n');
                            
                    codePre.className = codePre.className.replace(hiddenPreClass, '');
                            
                    languageSpan.parentNode.removeChild(languageSpan);
                });

                pres = Galleries.findElem(scriptBlock, 'pre');
                Galleries.iterateElem(labelElems, function(index, elem) {
                    var codePre = pres[index];
                    var labelSpan = elem;
                    if (index == 0) {
                        scriptBlock.activeTab = 0;
                    }
                    else {
                        labelSpan.className += otherTabClass;
                        codePre.className += hiddenPreClass;
                    }
                    Galleries.attachEventHandler(labelSpan, 'click', function(e) {
                        var activeTab = scriptBlock.activeTab;
                        labelElems[activeTab].className += otherTabClass;
                        pres[activeTab].className += hiddenPreClass;

                        codePre.className = codePre.className.replace(hiddenPreClass, '');
                        labelSpan.className = labelSpan.className.replace(otherTabClass, '');
                        scriptBlock.activeTab = index;
                    });
                });

                var preview = Galleries.findElem(scriptBlock, 'div', 'preview');
                if (preview.length == 0) {
                    preview.push(pres[pres.length - 1]);
                }
                Galleries.iterateElem(preview, function(index, elem) {
                    elem.parentNode.removeChild(elem);
                });

                if (window.clipboardData && clipboardData.setData) {
                    var copyLink = document.createElement('a');
                    copyLink.href = 'javascript:void(0);';
                    copyLink.className = 'copyCode';
                    copyLink.innerHTML = 'Copy code';
                    Galleries.attachEventHandler(copyLink, 'click', function (e) {
                        clipboardData.setData("Text", htmlDecode(labelElems[scriptBlock.activeTab].code));
                        return false;
                    });
                    scriptBlock.insertBefore(copyLink, scriptBlock.childNodes[0]);
                }
            }
        });
    }

    Galleries.onWindowLoad(function(){
        initializePage();
    });

</script>
<div id="longDesc">
    
<h2 class="endscriptcode">Introduction</h2>
<div>This sample demonstrates how you can track the current state of a StateMachine and the possible transitions using the following classes from
<a href="http://wf.codeplex.com/wikipage?title=Microsoft.Activities%20Overview&amp;referringTitle=Documentation">
Microsoft.Activities.Extensions</a>&nbsp;version 2.0.6 or higher.</div>
<ul>
<li>StateMachineStateTracker </li><li>StateTrackerPersistenceProvider </li></ul>
<h2>To use State Tracking with WorkflowApplication</h2>
<p><em>Note: For WorkflowServices see <a href="http://code.msdn.microsoft.com/Windows-Workflow-b9d5ccb7">
this sample</a></em></p>
<ol>
<li>Create an instance of the root activity </li><li>Create an instance of StateMachineStateTracker passing in the root activity </li><li>Create an instance of StateTrackerPersistenceProvider passing in the StateMachineStateTracker instance
</li><li>Add the StateMachineStateTracker and StateTrackerPersistenceProvider to the WorkflowApplication.Extensions
</li></ol>
<h3><div style="background:#FFFFCC;border:dotted 1px #FF0000;width:350px;height:300px;padding:10px;"><a href="http://i1.code.msdn.s-msft.com/windows-workflow-fee72008/image/file/55766/1/statetracker.wmv">http://i1.code.<wbr />msdn.s-msft.com<wbr />/windows-workfl<wbr />ow-fee72008/ima<wbr />ge/file/55766/1<wbr />/statetracker.w<wbr />mv</a></div>
<br>
<a id="http://i1.code.msdn.s-msft.com/windows-workflow-fee72008/image/file/55766/1/statetracker.wmv" href="http://i1.code.msdn.s-msft.com/windows-workflow-fee72008/image/file/55766/1/statetracker.wmv">Download video</a></h3>
<h3>Step 1: Create an instance of the root activity</h3>
<div>We recommend creating a readonly static for best performance</div>
<div>&nbsp;</div>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">internal static readonly Workflow1 Workflow1Definition = new Workflow1();
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">internal</span><span class="cs__keyword">static</span><span class="cs__keyword">readonly</span>&nbsp;Workflow1&nbsp;Workflow1Definition&nbsp;=&nbsp;<span class="cs__keyword">new</span>&nbsp;Workflow1();&nbsp;
</pre>
</div>
</div>
</div>
<div>&nbsp;</div>
<h3>Step 2: Create an instance of StateMachineStateTracker passing in the root <br>
activity</h3>
<h4><em>Note: You can also load an instate of StateMachineStateTracker from the InstanceStore</em></h4>
<h2 class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">public WorkflowInstance(IWorkflowView view, StateMachineStateTracker stateMachineStateTracker = null)
{
    this.view = view;
    this.StateTracker = stateMachineStateTracker ?? new StateMachineStateTracker(Workflow1Definition);
}
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">public</span>&nbsp;WorkflowInstance(IWorkflowView&nbsp;view,&nbsp;StateMachineStateTracker&nbsp;stateMachineStateTracker&nbsp;=&nbsp;<span class="cs__keyword">null</span>)&nbsp;
{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.view&nbsp;=&nbsp;view;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.StateTracker&nbsp;=&nbsp;stateMachineStateTracker&nbsp;??&nbsp;<span class="cs__keyword">new</span>&nbsp;StateMachineStateTracker(Workflow1Definition);&nbsp;
}&nbsp;
</pre>
</div>
</div>
</h2>
<h3>Step 3: Add the StateMachineStateTracker and StateTrackerPersistenceProvider to the WorkflowApplication.Extensions</h3>
<div>&nbsp;</div>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">private void CreateWorkflowApplication()
{
    this.Host = new WorkflowApplication(Workflow1Definition)
        {
            Idle = this.view.OnIdle, 
            Aborted = this.view.OnAbort, 
            Completed = this.view.OnComplete, 
            InstanceStore = this.view.InstanceStore, 
            OnUnhandledException = this.view.OnUnhandledException, 
            PersistableIdle = this.view.PersistableIdle, 
            Unloaded = this.view.OnUnload
        };

    // Setup tracking in UI
    this.Host.Extensions.Add(new MainWindow.TextTracker());
    this.Host.Extensions.Add(this.StateTracker);

    // Setup the persistence participant
    this.Host.Extensions.Add(new StateTrackerPersistenceProvider(this.StateTracker));
}
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">private</span><span class="cs__keyword">void</span>&nbsp;CreateWorkflowApplication()&nbsp;
{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Host&nbsp;=&nbsp;<span class="cs__keyword">new</span>&nbsp;WorkflowApplication(Workflow1Definition)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Idle&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.OnIdle,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Aborted&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.OnAbort,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Completed&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.OnComplete,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InstanceStore&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.InstanceStore,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OnUnhandledException&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.OnUnhandledException,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PersistableIdle&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.PersistableIdle,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unloaded&nbsp;=&nbsp;<span class="cs__keyword">this</span>.view.OnUnload&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__com">//&nbsp;Setup&nbsp;tracking&nbsp;in&nbsp;UI</span><span class="cs__keyword">this</span>.Host.Extensions.Add(<span class="cs__keyword">new</span>&nbsp;MainWindow.TextTracker());&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Host.Extensions.Add(<span class="cs__keyword">this</span>.StateTracker);&nbsp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__com">//&nbsp;Setup&nbsp;the&nbsp;persistence&nbsp;participant</span><span class="cs__keyword">this</span>.Host.Extensions.Add(<span class="cs__keyword">new</span>&nbsp;StateTrackerPersistenceProvider(<span class="cs__keyword">this</span>.StateTracker));&nbsp;
}&nbsp;
</pre>
</div>
</div>
</div>
<div>&nbsp;</div>
<h2>How do I determine which StateMachine is active?</h2>
<div>In WF4, a workflow may have multiple StateMachines. However, only one of them is active at a time.<br>
The StateMachineStateTracker keeps track of all state machines in the workflow in a dictionary that uses the display name of the StateMachine as a key.<br>
<strong>Note: Be sure to use unique names for each StateMachine activity or the tracker will not be able to track them properly
</strong></div>
<div><strong>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">// Get the name of the currently active StateMachine
Console.WriteLine(StateTracker.CurrentStateMachine);
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__com">//&nbsp;Get&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;currently&nbsp;active&nbsp;StateMachine</span>&nbsp;
Console.WriteLine(StateTracker.CurrentStateMachine);&nbsp;
</pre>
</div>
</div>
</div>
</strong></div>
<div>&nbsp;</div>
<h2>How do I know what state the currently active state machine is in?</h2>
<div>When&nbsp; the Workflow changes state in a StateMachine, a tracking record of type StateMachineStateRecord is emitted. This tracking record is captured by the StateMachineStateTracker. To access the last known state of the state machine</div>
<h2>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">// Get the current state of the currently active StateMachine
Console.WriteLine(StateTracker.CurrentState);
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__com">//&nbsp;Get&nbsp;the&nbsp;current&nbsp;state&nbsp;of&nbsp;the&nbsp;currently&nbsp;active&nbsp;StateMachine</span>&nbsp;
Console.WriteLine(StateTracker.CurrentState);&nbsp;
</pre>
</div>
</div>
</div>
<div class="endscriptcode"></div>
</h2>
<div class="endscriptcode">
<h2>How do I know what the possible transitions are?</h2>
<div>To determine last known set of possible transitions</div>
</div>
<p>&nbsp;</p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">// Get a comma delimited list of display names for possible transitions for the current state
Console.WriteLine(StateTracker.PossibleTransitions);

foreach(var transition in StateTracker.Transitions)
{
   // Access the Transition and the trigger activity
   Console.WriteLine(transition.Trigger.DisplayName)
}
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__com">//&nbsp;Get&nbsp;a&nbsp;comma&nbsp;delimited&nbsp;list&nbsp;of&nbsp;display&nbsp;names&nbsp;for&nbsp;possible&nbsp;transitions&nbsp;for&nbsp;the&nbsp;current&nbsp;state</span>&nbsp;
Console.WriteLine(StateTracker.PossibleTransitions);&nbsp;
&nbsp;
<span class="cs__keyword">foreach</span>(var&nbsp;transition&nbsp;<span class="cs__keyword">in</span>&nbsp;StateTracker.Transitions)&nbsp;
{&nbsp;
&nbsp;&nbsp;&nbsp;<span class="cs__com">//&nbsp;Access&nbsp;the&nbsp;Transition&nbsp;and&nbsp;the&nbsp;trigger&nbsp;activity</span>&nbsp;
&nbsp;&nbsp;&nbsp;Console.WriteLine(transition.Trigger.DisplayName)&nbsp;
}&nbsp;
</pre>
</div>
</div>
</div>
<p>&nbsp;</p>
<h2>How is the state tracking information persisted in the instance <br>
store?</h2>
<p>The StateTrackerPersistenceProvider class creates an XML document with the list of state machines and transitions. This document is stored in the InstancePromotedPropertiesTable and is accessible via the InstancePromotedProperties view. The persistence particpant
 stores values as follows<br>
<strong>Promotion Name:</strong> StateTracker<br>
<strong>Value1:</strong> XML Document<br>
<strong>Value2:</strong> Activity ID<br>
<strong>Value3:</strong> Display Name</p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>XML</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">xml</span>
<pre class="hidden">&lt;StateMachines CurrentStateMachine=&quot;StateMachine&quot;&gt;
  &lt;StateMachine Name=&quot;StateMachine&quot; CurrentState=&quot;State2&quot;&gt;
    &lt;Transition&gt;T3&lt;/Transition&gt;
    &lt;Transition&gt;T5&lt;/Transition&gt;
  &lt;/StateMachine&gt;
&lt;/StateMachines&gt;
</pre>
<div class="preview">
<pre class="xml"><span class="xml__tag_start">&lt;StateMachines</span><span class="xml__attr_name">CurrentStateMachine</span>=<span class="xml__attr_value">&quot;StateMachine&quot;</span><span class="xml__tag_start">&gt;&nbsp;
</span><span class="xml__tag_start">&lt;StateMachine</span><span class="xml__attr_name">Name</span>=<span class="xml__attr_value">&quot;StateMachine&quot;</span><span class="xml__attr_name">CurrentState</span>=<span class="xml__attr_value">&quot;State2&quot;</span><span class="xml__tag_start">&gt;&nbsp;
</span><span class="xml__tag_start">&lt;Transition</span><span class="xml__tag_start">&gt;</span>T3<span class="xml__tag_end">&lt;/Transition&gt;</span><span class="xml__tag_start">&lt;Transition</span><span class="xml__tag_start">&gt;</span>T5<span class="xml__tag_end">&lt;/Transition&gt;</span><span class="xml__tag_end">&lt;/StateMachine&gt;</span><span class="xml__tag_end">&lt;/StateMachines&gt;</span></pre>
</div>
</div>
</div>
<p>&nbsp;</p>
<h2>How do I initialize a state tracker from a persisted instance?</h2>
<p>Use the StateMachineStateTracker.LoadInstances method to return a collection of all StateMachineStateTracker Instances found for the given activity definition.<span id="mce_marker">&nbsp;</span></p>
<h2>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>C#</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span></div>
<span class="hidden">csharp</span>
<pre class="hidden">internal WorkflowModel(IWorkflowView view)
{
    this.view = view;
    this.Workflows = new ObservableCollection&lt;WorkflowInstance&gt;();
    var instances = StateMachineStateTracker.LoadInstances(
        WorkflowInstance.Workflow1Definition, view.ConnectionString);

    if (instances != null)
    {
        foreach (var stateMachineStateTracker in instances)
        {
            this.Workflows.Add(new WorkflowInstance(this.view, stateMachineStateTracker));
        }

        Trace.WriteLine(
            string.Format(
                &quot;Loaded {0} instance{1} from instance store&quot;,
                this.Workflows.Count,
                this.Workflows.Count != 1 ? &quot;s&quot; : string.Empty));
    }
    else
    {
        Trace.WriteLine(&quot;Error loading instances - be sure to create the SampleInstanceStore database first&quot;);
    }
}
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">internal</span>&nbsp;WorkflowModel(IWorkflowView&nbsp;view)&nbsp;
{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.view&nbsp;=&nbsp;view;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Workflows&nbsp;=&nbsp;<span class="cs__keyword">new</span>&nbsp;ObservableCollection&lt;WorkflowInstance&gt;();&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;instances&nbsp;=&nbsp;StateMachineStateTracker.LoadInstances(&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WorkflowInstance.Workflow1Definition,&nbsp;view.ConnectionString);&nbsp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">if</span>&nbsp;(instances&nbsp;!=&nbsp;<span class="cs__keyword">null</span>)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">foreach</span>&nbsp;(var&nbsp;stateMachineStateTracker&nbsp;<span class="cs__keyword">in</span>&nbsp;instances)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Workflows.Add(<span class="cs__keyword">new</span>&nbsp;WorkflowInstance(<span class="cs__keyword">this</span>.view,&nbsp;stateMachineStateTracker));&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trace.WriteLine(&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">string</span>.Format(&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__string">&quot;Loaded&nbsp;{0}&nbsp;instance{1}&nbsp;from&nbsp;instance&nbsp;store&quot;</span>,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Workflows.Count,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">this</span>.Workflows.Count&nbsp;!=&nbsp;<span class="cs__number">1</span>&nbsp;?&nbsp;<span class="cs__string">&quot;s&quot;</span>&nbsp;:&nbsp;<span class="cs__keyword">string</span>.Empty));&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="cs__keyword">else</span>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trace.WriteLine(<span class="cs__string">&quot;Error&nbsp;loading&nbsp;instances&nbsp;-&nbsp;be&nbsp;sure&nbsp;to&nbsp;create&nbsp;the&nbsp;SampleInstanceStore&nbsp;database&nbsp;first&quot;</span>);&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
}&nbsp;
</pre>
</div>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
</h2>

</div>


    </div>
</body>
</html>
